package 力扣题库;

import java.util.Stack;

public class p20有效括号 {

    public boolean isValid(String s) {
        Stack<Character> stack=new Stack<Character>();

        if (s.length() % 2  != 0) {
            return false;
        }

        for(char c:s.toCharArray()) {
            if (c=='(') {
                stack.push(')');
            }else if (c=='[') {
                stack.push(']');
            }else if (c=='{') {
                stack.push('}');
            }else if (stack.isEmpty() || c!=stack.pop()) {
                return false;
            }
        }

        if (stack.isEmpty()) {
            return true;
        }

        return false;//全是左括号
    }

}
